#include "boot.h"

// DO NOT DEFINE ANY NON-LOCAL VARIBLE!

void load_kernel() {
  
  // remove both lines above before write codes below
  Elf32_Ehdr *elf = (void *)0x8000;
  copy_from_disk(elf, 255 * SECTSIZE, SECTSIZE);
  Elf32_Phdr *ph, *eph;
  ph = (void*)((uint32_t)elf + elf->e_phoff);
  eph = ph + elf->e_phnum;
  for (; ph < eph; ph++) {
    if (ph->p_type == PT_LOAD) {
      // TODO: Lab1-2, Load kernel and jump
      void *dst1 = (void*)(ph->p_vaddr);
      void *src = (void*)((uint32_t)elf + ph->p_offset);
      uint32_t filesz_p = ph->p_filesz;
      memcpy(dst1, src, filesz_p);
      
      void *dst2 = (void*)(ph->p_vaddr + ph->p_filesz);
      uint32_t filesz_0 = ph->p_memsz - ph->p_filesz;
      memset(dst2, 0, filesz_0);
     
    }
  }
  uint32_t entry = (uint32_t)(elf->e_entry); // change me
  ((void(*)())entry)();
}
